import pandas as pd

from models.stock_model import SHDayInfo


def get_sh_low_n_date(end_date, low_n=10):
    """
    获取最近一次 low_n 底日期
    """
    df_sh = pd.read_csv('shanghai_index.csv')
    for row2 in df_sh.index:
        arr_di = [SHDayInfo(df_sh.loc[i]) for i in range(row2, row2 + low_n)]
        arr_di_low = [di.low for di in arr_di]
        if eval(arr_di[0].trade_date) > end_date:
            continue
        if min(arr_di_low) == arr_di_low[0]:
            return eval(arr_di[0].trade_date)
    return None


def get_sh_top_n_date(end_date, top_n=10):
    """
    获取最近一次 top_n 顶日期
    """
    df_sh = pd.read_csv('shanghai_index.csv')
    for row2 in df_sh.index:
        arr_di = [SHDayInfo(df_sh.loc[i]) for i in range(row2, row2 + top_n)]
        arr_di_h = [di.high for di in arr_di]
        if eval(arr_di[0].trade_date) > end_date:
            continue
        if max(arr_di_h) == arr_di_h[0]:
            return eval(arr_di[0].trade_date)
    return None


def get_sh_today_date():
    """
    获取今日日期
    """
    df_sh = pd.read_csv('shanghai_index.csv')
    for row2 in df_sh.index:
        today_di = SHDayInfo(df_sh.loc[row2])
        return eval(today_di.trade_date)
    return None


if __name__ == '__main__':
    top_n = 30
    max_date = get_sh_top_n_date(top_n)
    print(max_date)
